/**
 * 这里放入了请求后端接口的jS文件
 */

// 务必这样写
import {
    useAxios
} from './index'
import {
    useTokenStore,
} from '../store/token';


let tokenStore = useTokenStore();
// const token = tokenStore.token
//     // console.log("存储的token:", token);

// 请求前缀
let axios = useAxios({
    baseURL: "http://lzfblog.cn:8888",
});



// 用户注册
function userRegister(username, password, nickname, email, code) {
    return axios.post("/user/register", {
        username,
        password,
        nickname,
        email,
        code,
    })
}

// 获取用户注册验证码
function getEmailCode(email) {
    return axios.post("/mail/send/register", {
        email,
    })
}


// 获取同学地图 传递 一个 当前用户id 参数
function getFriendMap(uid) {
    return axios.get(`/friendMap/${uid}`, {
        headers: {
            "Authorization": `${tokenStore.token}`
        }
    })
}


// 用户登录接口
function login(username, password) {
    return axios.post("/user/login", {
        username,
        password,
    })
}

// 获取登录的用户详细信息
function getUserDetail(username) {
    return axios.post('/user/detail', {
        username,
    }, {
        headers: {
            "Authorization": `${tokenStore.token}`
        }
    })
}

//用户注册接口


function register(username, password, nickname, email, code) {
    return axios.post("/user/login", {
        username,
        password,
        nickname,
        email,
        code
    })
}






// 获取当前用户所有朋友
function getFriendList(id) {

    // 携带 token 才能正常访问
    return axios.get(`/friend/${id}`, {
        headers: {
            "Authorization": `${tokenStore.token}`
        }
    })
}


// 添加一个朋友

function inserFriend(gender, Id, phone, province, remark, uid, username) {
    return axios.post(`/friend`, {

        gender,
        Id,
        phone,
        province,
        remark,
        "uid": parseInt(uid),
        username
    }, {
        headers: {
            "Authorization": `${tokenStore.token}`
        }
    })
}

// 修改朋友信息
function updateFriendById(id, username, gender, phone, province, remark) {
    return axios.put(`/friend/${id}`, {
        gender,
        phone,
        province,
        remark,
        username,
    }, {
        headers: {
            "Authorization": `${tokenStore.token}`
        }
    })

}

// 删除一个朋友
function deleteFriendById(id) {
    return axios.delete(`/friend/${id}`, {
        headers: {
            "Authorization": `${tokenStore.token}`
        }
    })
}

// 获取留言信息
function getMessageList() {
    return axios.get("/message", {
        headers: {
            "Authorization": `${tokenStore.token}`
        }
    })
}

// 获取上传照片信息
function getImageList(id) {
    return axios.get(`/images/${id}`, {
        headers: {
            "Authorization": `${tokenStore.token}`
        }
    })
}


function uploadImage(payload) {
    return axios.post("/image/upload", payload, {
        headers: {
            // 文件上传协议头
            "Content-type": "multipart/form-data",
            "Authorization": `${tokenStore.token}`
        }
    })
}

// 删除图片
function deleteImage(fileName) {
    return axios.delete(`/image/${fileName}`, {
        headers: {
            "Authorization": `${tokenStore.token}`
        }
    })
}


// 添加一条留言
function postMessage(uid, content) {
   return  axios.post("/message", {
        uid,
        content
    }, {
        headers: {
            "Authorization": `${tokenStore.token}`
        }
    })
}

export default {
    postMessage,
    deleteImage,
    uploadImage,
    getImageList,
    getMessageList,
    userRegister,
    getEmailCode,
    getFriendMap,
    getUserDetail,
    login,
    register,
    getFriendList,
    inserFriend,
    updateFriendById,
    deleteFriendById
}